from modules import reviews
from common import ReviewEntity, AlbumEntity
from dataaccess import AlbumData 
from businessrules import AlbumBiz, ReviewBiz

def submit():
    """/radio/reviews/submit/album_id
    review an album with staff privileges"""
    album_id = request.args(0)
    album = AlbumData.AlbumData().SelectAlbumByID(album_id)
    if album == None:
        redirect(URL("default","index"))

    if(request.env.request_method == "POST"):
#        album = AlbumEntity.AlbumEntity(albumID=album_id,
#                                artistName=request.vars.get("artist"),
#                                albumName=request.vars.get("album"),
#                                promoter=request.vars.get("promoter","Unknown"),
#                                genre=request.vars.get("genre", "Unknown"),
#                                description= request.vars.get("description"))

        rotation = request.vars.get("rotation",False)
        if rotation:
            album.Rotation=rotation
            AlbumBiz.AlbumBiz().UpdateAlbum(album)

        review = ReviewEntity.ReviewEntity(albumID=album_id,
                                        reviewer=auth.user.email,
                                        reviewerComment=album.Description,
                                        director=None)

        ReviewBiz.ReviewBiz().InsertReview(review)

        redirect(URL('reviews','index',vars=dict(submitted_review=True)))

    return reviews.submit(album)

def index():
    """/radio/reviews/index
    view all the reviewable albums"""
    success = request.vars.get('submitted_review',False)
    return reviews.index(success)

def admin():
    """/radio/reviews/admin
    view all the reviewable albums with director privileges"""
    redirect(URL('reviews','index',args=('director')))

def approve():
    """/radio/reviews/approve/album_id
    review an album with director privileges"""
    album_id = request.args(0)
    return reviews.approve(album_id) 
